package com.gz.mylife.util;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;

import android.util.Log;

import com.gz.mylife.App;

public class SendUdp {

	/**
	 * 发送查找设备
	 */
	public static void sendmessagefind(){
		new Thread(){public void run() {
			App.type="find";
			toSendmessage("::FF:FF:FF:FF:FF:FF,FIND", "6996", "255.255.255.255",App.type);
		};}.start();
	}


	public  static void sendmessageopen(final String message,final String ip){
		new Thread(){public void run() {
			Log.i("ch", "走open方法");
			App.type="open";
			toSendmessage(message, "6996", ip,App.type);
		};}.start();
	}


	/**
	 *
	 * @param message  发送内容
	 * @param port     端口号
	 * @param ip        ip地址
	 * type find 和send、  find是查设备 send是发送开门
	 */ 
	private  static void toSendmessage(String message,String port,String ip,String type) {
		try {
			Log.i("chenghao","服务发送message＝"+message);
			Log.i("chenghao","服务发送port＝"+port);
			Log.i("chenghao","服务发送ip＝"+ip);
			Log.i("chenghao","type＝"+type);
			//ds = new DatagramSocket(5000);
			//数据
			byte[] buf= message.getBytes();
			InetAddress destination = null ;
			//需要发送的地址
			destination = InetAddress.getByName(ip);
			int pro=Integer.parseInt(port);
			//(1), 内容 2，内容长度，3 ip地址， 4端口号
			DatagramPacket dp =new DatagramPacket(buf, buf.length, destination , pro);
			if(App.ds!=null){
				App.ds.send(dp);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
